package com.masterclient.app.modules.goods.adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.masterclient.app.App;
import com.masterclient.app.R;
import com.masterclient.app.base.MainViewHolder;
import com.masterclient.app.modules.goods.activity.CommentActivity;
import com.eoner.baselibrary.bean.goods.CommodityComment;
import com.eoner.baselibrary.bean.goods.CommodityCommentContent;
import com.eoner.baselibrary.bean.goods.CommodityDetailData;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.masterclient.app.widget.CircleTransform;

import java.util.List;

public class CommodityDetailCommentAdapter extends DelegateAdapter.Adapter<MainViewHolder> {

    Activity mContext;

    private LayoutHelper mLayoutHelper;


    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int mCount = 0;

    CommodityDetailData data;


    public CommodityDetailCommentAdapter(Activity context, LayoutHelper layoutHelper, int count) {
        this(context, layoutHelper, count, null,null);
    }

    public CommodityDetailCommentAdapter(Activity context, LayoutHelper layoutHelper, int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams, CommodityDetailData data) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.mCount = count;
        this.mLayoutParams = layoutParams;
        this.data = data;
    }
    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }
    @Override
    protected void onBindViewHolderWithOffset(MainViewHolder holder, int position, int offsetTotal) {

    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }
    @Override
    public void onBindViewHolder(MainViewHolder holder, int position) {
        if(mLayoutParams!=null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

        TextView tv_comment_num = holder.itemView.findViewById(R.id.tv_comment_num);
        TextView tv_username = holder.itemView.findViewById(R.id.tv_username);
        TextView tv_comment = holder.itemView.findViewById(R.id.tv_comment);
        TextView tv_time = holder.itemView.findViewById(R.id.tv_time);
        ImageView iv_head = holder.itemView.findViewById(R.id.iv_head);
        View ll_comment = holder.itemView.findViewById(R.id.ll_comment);
        View tv_comment_all = holder.itemView.findViewById(R.id.tv_comment_all);
        View comment_head_layout = holder.itemView.findViewById(R.id.comment_head_layout);

        CommodityComment commodityComment = data.getComment();

        tv_comment_num.setText("商品评价(" + commodityComment.getTotal_count() + ")");
        List<CommodityCommentContent> contents = commodityComment.getContents();
        if (contents != null && contents.size() > 0) {
            CommodityCommentContent content = contents.get(0);
            if (!TextUtils.isEmpty(content.getAvatar())) {
                App.picasso.load(content.getAvatar()).resize(ScreenUtils.dp2px(36), ScreenUtils.dp2px(36)).transform(new CircleTransform()).into(iv_head);
            }
            tv_username.setText(content.getNickname());
            tv_comment.setText(content.getDetail());
            if (!TextUtils.isEmpty(content.getShow_at())){// && content.getShow_at().length() > 10
                tv_time.setText(content.getShow_at());
            }
        } else {
            ll_comment.setVisibility(View.GONE);
            tv_comment_num.setText("暂无评价");
            tv_comment_all.setVisibility(View.GONE);
        }

        comment_head_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (contents != null && contents.size() > 0) {
                    Intent intent = new Intent(mContext, CommentActivity.class);
                    intent.putExtra("id",data.getProduct_id());
                    mContext.startActivity(intent);
                }
            }
        });


    }


    @Override
    public int getItemCount() {
        return mCount;
    }

    @Override
    public int getItemViewType(int position) {
        return 4;
    }
}
